using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.IO;

namespace BodySoundSimulator.Training.CallibrationWizard
{
	class CallibrationData
	{
		#region fields
		Rectangle centerMarkerBoundry;
		Rectangle leftMarkerBoundry;
		Rectangle rightMarkerBoundry;
		Rectangle stethMarkerBoundry;
		Point jugularNotchLocation;
		Point xiphoidProcessLocation;
		Point leftCostalMarginLocation;
		Point rightCostalMarginLocation;
		#endregion

		#region Properties
		public Rectangle CenterMarkerBoundry
		{
			get { return centerMarkerBoundry; }
			set { centerMarkerBoundry = value; }
		}

		public Rectangle LeftMarkerBoundry
		{
			get { return leftMarkerBoundry; }
			set { leftMarkerBoundry = value; }
		}

		public Rectangle RightMarkerBoundry
		{
			get { return rightMarkerBoundry; }
			set { rightMarkerBoundry = value; }
		}

		public Rectangle StethMarkerBoundry
		{
			get { return stethMarkerBoundry; }
			set { stethMarkerBoundry = value; }
		}

		public Point JugularNotchLocation
		{
			get { return jugularNotchLocation; }
			set { jugularNotchLocation = value; }
		}

		public Point XiphoidProcessLocation
		{
			get { return xiphoidProcessLocation; }
			set { xiphoidProcessLocation = value; }
		}

		public Point LeftCostalMarginLocation
		{
			get { return leftCostalMarginLocation; }
			set { leftCostalMarginLocation = value; }
		}

		public Point RightCostalMarginLocation
		{
			get { return rightCostalMarginLocation; }
			set { rightCostalMarginLocation = value; }
		}
		#endregion

		#region Constructors
		public CallibrationData()
		{
			this.centerMarkerBoundry = new Rectangle(0, 0, 5, 5);
			this.leftMarkerBoundry = new Rectangle(20, 20, 5, 5);
			this.rightMarkerBoundry = new Rectangle(20, 25, 5, 5);
			this.stethMarkerBoundry = new Rectangle(20, 25, 5, 5);
			this.jugularNotchLocation = new Point(10, 10);
			this.xiphoidProcessLocation = new Point(20, 20);
			this.leftCostalMarginLocation = new Point(30, 20);
			this.rightCostalMarginLocation = new Point(50, 50);
		}
		#endregion

		#region Public methods
		public CallibrationData Clone()
		{
			CallibrationData clone = new CallibrationData();
			clone.centerMarkerBoundry = new Rectangle(this.centerMarkerBoundry.X, this.centerMarkerBoundry.Y, this.centerMarkerBoundry.Width, this.centerMarkerBoundry.Height);
			clone.leftMarkerBoundry = new Rectangle(this.leftMarkerBoundry.X, this.leftMarkerBoundry.Y, this.leftMarkerBoundry.Width, this.leftMarkerBoundry.Height);
			clone.rightMarkerBoundry = new Rectangle(this.rightMarkerBoundry.X, this.rightMarkerBoundry.Y, this.rightMarkerBoundry.Width, this.rightMarkerBoundry.Height);
			clone.stethMarkerBoundry = new Rectangle(this.stethMarkerBoundry.X, this.stethMarkerBoundry.Y, this.stethMarkerBoundry.Width, this.stethMarkerBoundry.Height);
			clone.jugularNotchLocation = new Point(this.jugularNotchLocation.X, this.jugularNotchLocation.Y);
			clone.xiphoidProcessLocation = new Point(this.xiphoidProcessLocation.X, this.xiphoidProcessLocation.Y);
			clone.leftCostalMarginLocation = new Point(this.leftCostalMarginLocation.X, this.leftCostalMarginLocation.Y);
			clone.rightCostalMarginLocation = new Point(this.rightCostalMarginLocation.X, this.rightCostalMarginLocation.Y);
			return clone;
		}

		public byte[] GetBytes()
		{
			List<byte> bytes = new List<byte>();
			MemoryStream mem = new MemoryStream();
			StreamWriter stream = new StreamWriter(mem);
			bytes.AddRange(ToBytes(this.centerMarkerBoundry.X));
			bytes.AddRange(ToBytes(this.centerMarkerBoundry.Y));
			bytes.AddRange(ToBytes(this.centerMarkerBoundry.Width));
			bytes.AddRange(ToBytes(this.centerMarkerBoundry.Height));
			bytes.AddRange(ToBytes(this.leftMarkerBoundry.X));
			bytes.AddRange(ToBytes(this.leftMarkerBoundry.Y));
			bytes.AddRange(ToBytes(this.leftMarkerBoundry.Width));
			bytes.AddRange(ToBytes(this.leftMarkerBoundry.Height));
			bytes.AddRange(ToBytes(this.rightMarkerBoundry.X));
			bytes.AddRange(ToBytes(this.rightMarkerBoundry.Y));
			bytes.AddRange(ToBytes(this.rightMarkerBoundry.Width));
			bytes.AddRange(ToBytes(this.rightMarkerBoundry.Height));
			bytes.AddRange(ToBytes(this.stethMarkerBoundry.X));
			bytes.AddRange(ToBytes(this.stethMarkerBoundry.Y));
			bytes.AddRange(ToBytes(this.stethMarkerBoundry.Width));
			bytes.AddRange(ToBytes(this.stethMarkerBoundry.Height));
			bytes.AddRange(ToBytes(this.jugularNotchLocation.X));
			bytes.AddRange(ToBytes(this.jugularNotchLocation.Y));
			bytes.AddRange(ToBytes(this.xiphoidProcessLocation.X));
			bytes.AddRange(ToBytes(this.xiphoidProcessLocation.Y));
			bytes.AddRange(ToBytes(this.leftCostalMarginLocation.X));
			bytes.AddRange(ToBytes(this.leftCostalMarginLocation.Y));
			bytes.AddRange(ToBytes(this.rightCostalMarginLocation.X));
			bytes.AddRange(ToBytes(this.rightCostalMarginLocation.Y));

			return bytes.ToArray();
		}
		#endregion

		#region Private methods
		byte[] ToBytes(int value)
		{
			byte[] intBytes = BitConverter.GetBytes(value);
			if (BitConverter.IsLittleEndian)
			{
				Array.Reverse(intBytes);
			}
			return intBytes;
		}
		#endregion
	}
}
